﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using Aspose.Excel;

public partial class StudentsPublic : BasePage
{
    protected override void pageLoad()
    {
        if (!IsPostBack)
        {
            Fill();
        }
    }

    public override string Icon
    {
        get
        {
            return "images/admin_icon.png";
        }
    }

    protected void lbExport_Click(object sender, ImageClickEventArgs e)
    {
        ExportToExcel();
        /*DataLayer dal = new DataLayer();
        string message = string.Empty;

        DataTable toExcel;
        if (tbName.Text == string.Empty)
            toExcel = dal.FindStudents(PAConfiguration.ConnectionString, Letter, out message);
        else
            toExcel = dal.FindStudents(PAConfiguration.ConnectionString, tbName.Text, out message);
    
        PALogManager.MessageLog(message);

        DataColumn student_id = null;
        foreach (DataColumn dc in toExcel.Columns)
        {
            foreach (GridColumn dgf in rgStudents.Columns)
            {
                if (dc.ColumnName == dgf.SortExpression)
                {
                    dc.ColumnName = dgf.HeaderText;
                    break;
                }
				
            }

            if (dc.ColumnName == "STUDENT_ID")
            {
                student_id = dc;
            }
        }

        if (student_id != null)
        {
            toExcel.Columns.Remove(student_id);
        }
        Manager.ExportToCSV(
            toExcel,
            this.Context,
            "Students.csv");*/
    }


    protected char Letter
    {
        get
        {
            if (String.IsNullOrEmpty(Request.QueryString["letter"]))
            {
                return 'A';
            }
            if (string.Compare(Request.QueryString["letter"], "all", true) == 0)
                return ' ';

            return (char)Request.QueryString["letter"][0];
        }
    }

    protected override void Fill()
    {
        DataLayer dal = new DataLayer();
        string message;

        DataTable dt = null;
        var initial = string.IsNullOrEmpty(tbName.Text) ? Letter.ToString() : tbName.Text;

        dt = dal.FindStudents(PAConfiguration.ConnectionString, initial, Teacher.TEACHER_ID, true, out message);

        //if(tbName.Text == string.Empty)
        //    dt = dal.FindStudents(PAConfiguration.ConnectionString, Letter, out message);
        //else
        //    dt = dal.FindStudents(PAConfiguration.ConnectionString, tbName.Text, out message);

        if (dt == null)
        {
            rgStudents.DataSource = null;
            rgStudents.DataBind();
            return;
        }


        if (!string.IsNullOrEmpty(Sorting))
        {
            dt.DefaultView.Sort = Sorting;
        }

        rgStudents.DataSource = dt;
        rgStudents.DataBind();

    }

    protected void gvStudents_Sorting(object sender, GridViewSortEventArgs e)
    {
        Sort(e);
    }

    protected void gvStudents_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            this.ApplySortingToHeader(e.Row.Cells);
        }
    }
    protected void bGo_Click(object sender, ImageClickEventArgs e)
    {
        Fill();
    }


    protected void rgStudents_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)
    {
        Sort(e);
    }

    protected void rgStudents_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
    {
        Fill();
    }

    void ExportToExcel()
    {
		try
		{
			License license = new License();
			string path = Server.MapPath("Aspose.Excel.lic");
			license.SetLicense(path);

			Excel ex = new Excel();
			Worksheet ws = ex.Worksheets[0];

			DataLayer dal = new DataLayer();
			string message = string.Empty;

			DataTable dt = null;
			var initial = string.IsNullOrEmpty(tbName.Text) ? Letter.ToString() : tbName.Text;

			dt = dal.FindStudents(PAConfiguration.ConnectionString, initial, Teacher.TEACHER_ID, true, out message);

			//if (tbName.Text == string.Empty)
			//  dt = dal.FindStudents(PAConfiguration.ConnectionString, Letter, out message);
			//else
			//  dt = dal.FindStudents(PAConfiguration.ConnectionString, tbName.Text, out message);

			int startRow = 0;

			Utils.GridToExcel(dt, rgStudents, ws, true, ref startRow);

			ex.Save("Students.xls", FileFormatType.Excel2003, SaveType.OpenInExcel, HttpContext.Current.Response);
			HttpContext.Current.Response.End();
		}
		catch(Exception ex)
		{
			PALogManager.ExceptionLog(ex);
		}
    }
}
